const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        showShade: false,
        searchType: 'month',
        list: [],
        totals: {},
        page: 1,
        hasMore: true,
        size: 10
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        this.setData(options)
        this.getAgentInfo();
        this.setData({monthIndex: 5})
    },
    getAgentInfo() {
        let level = this.data.level;
        console.log(level)
        let _this = this
        App._get('index/agent_info/level/' + level, {}, function (result) {
            console.log(result.data)
            _this.setData(result.data)
            wx.setNavigationBarTitle({
                title: result.data.agentInfo.name + '业绩统计'
            })
        })
    },
    onPullDownRefresh() {
        //请求数据更新
        console.log('refresh')
        this.setData({page: 1, hasMore: true, list: []})
        this.loadAgentsCalculate();
    },
    onReachBottom(event) {
        //请求更多数据
        console.log('load more')
        this.setData({
            page: this.data.page + 1
        });
        this.loadAgentsCalculate();
    },
    onChange(e) {
        console.log(e.detail)
        this.setData(e.detail)
        this.setData({showShade: false})
        this.onPullDownRefresh();
    },
    onConditionToggle(e) {
        console.log(e.detail.conditionSearch)
        this.setData({showShade: e.detail.conditionSearch})
    },
    getSearchConditions() {
        return {
            searchType: this.data.searchType,
            years: this.data.years,
            months: this.data.months,
            seasonIndex: this.data.seasonIndex,
            monthIndex: this.data.monthIndex,
            yearIndex: this.data.yearIndex,
            level: this.data.level,
            page: this.data.page,
            size: this.data.size
        }
    },
    loadAgentsCalculate() {
        let _this = this
        let data = this.getSearchConditions()
        App._post_form('index/agent_calculate', data, function (result) {
            if (result.data.list.length < _this.data.size) {
                console.log("没有更多数据了")
                _this.setData({
                    'hasMore': false,
                    'isLoading': false,
                });
            }
            let rows = _this.data.list.concat(result.data.list);
            _this.setData({
                page: _this.data.page + 1,
                list: rows,
                'isLoading': false,
                'totals': result.data.totals,
            })
        })
    },
    navigateToAgentSale(e) {
        let userId = e.currentTarget.dataset.user_id;
        var url = '/pages/agent/calculate/agent?userId=' + userId + '&monthIndex=' + this.data.monthIndex
            + '&yearIndex=' + this.data.yearIndex + '&seasonIndex=' + this.data.seasonIndex + '&searchType=' + this.data.searchType
            + '&level=' + this.data.level;
        console.log(url)
        wx.navigateTo({
            url: url,
        });
    }

});